<?php
namespace app\rental\controller;

use app\rental\model\RentalBrandModel;
use app\rental\model\RentalCarModel;
use app\rental\model\RentalCategoryModel;
use app\rental\model\RentalEmissionModel;
use app\rental\model\RentalPriceRangeModel;
use app\rental\model\RentalRankModel;
use app\rental\model\RentalYearModel;
use cmf\controller\AdminBaseController;
use think\Db;

class AdminCarController extends AdminBaseController
{
    /**
     * 汽车列表
     * @adminMenu(
     *     'name'   => '汽车管理',
     *     'parent' => 'rental/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 1,
     *     'icon'   => '',
     *     'remark' => '汽车列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();
        /*保留筛选条件*/
        if (empty($param)) {
            $param = Cookie('__param__');
        }
        if ($param != Cookie('__param__')) {
            Cookie('__param__', $param);
        }
        $categoryId = $this->request->param('category', 0, 'intval');
        $this->assign('category', $categoryId);
        $BrandId = $this->request->param('brand', 0, 'intval');
        $this->assign('brand', $BrandId);
        $RankId = $this->request->param('rank', 0, 'intval');
        $this->assign('rank', $RankId);
        $PriceRangeId = $this->request->param('price_range', 0, 'intval');
        $this->assign('price_range', $PriceRangeId);
        $intId = $this->request->param('ser_car_id', 0, 'intval');

        $where = [];
        if (!empty($categoryId)) {
            $where['category_id'] = ['eq', $categoryId];
        }
        if (!empty($BrandId)) {
            $where['brand_id'] = ['eq', $BrandId];
        }
        if (!empty($intId)) {
            $where['id'] = ['eq', $intId];
            $this->assign('ser_car_id', $intId);
        }
        if (!empty($PriceRangeId)) {
            $where['price_range_id'] = ['eq', $PriceRangeId];
        }
        if (!empty($RankId)) {
            $where['rank'] = ['eq', $RankId];
        }
        if (isset($param['start_time']) && !empty($param['start_time'])) {
            $where['update_time'] = ['gt', $param['start_time']];
        }
        if (isset($param['end_time']) && !empty($param['end_time'])) {
            $where['update_time'] = ['lt', $param['end_time']];
        }
        if (isset($param['keyword']) && !empty($param['keyword'])) {
            $where['title'] = ['like', '%' . $param['keyword'] . '%'];
        }

        $where['delete_time'] = ['eq', 0];
        $rentalCarModel = new RentalCarModel();
        $cars = $rentalCarModel->where($where)->order("list_order desc")->paginate();
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('cars', $cars);
        $this->assign($param);
        $this->assign('page', $cars->render());
        $this->getSelectAttr();
        return $this->fetch();
    }

    public function getSelectAttr()
    {
        $rentalCategoryModel = new RentalCategoryModel();
        $categoryTree = $rentalCategoryModel->order('list_order desc')->where('status', '1')->select();
        $this->assign('category_tree', $categoryTree);
        $rentalBrandModel = new RentalBrandModel();
        $brand = $rentalBrandModel->where('status', '1')->select();
        $this->assign('brands', $brand);
        $rentalPriceRangeModel = new RentalPriceRangeModel();
        $priceRange = $rentalPriceRangeModel->select();
        $this->assign('price_ranges', $priceRange);
        $rentalRankModel = new RentalRankModel();
        $rank = $rentalRankModel->select();
        $this->assign('ranks', $rank);
        $rentalYearModel = new RentalYearModel();
        $year = $rentalYearModel->select();
        $this->assign('years', $year);
        $rentalEmissionModel = new RentalEmissionModel();
        $emission = $rentalEmissionModel->select();
        $this->assign('emissions', $emission);
    }

    /**
     * 添加汽车
     * @adminMenu(
     *     'name'   => '添加汽车',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加汽车',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $this->getSelectAttr();
        return $this->fetch();
    }

    /**
     * 添加汽车提交
     * @adminMenu(
     *     'name'   => '添加汽车提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 1,
     *     'icon'   => '',
     *     'remark' => '添加汽车提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $result = $this->validate($data, 'RentalCar');
            if ($result !== true) {
                $this->error($result);
            }

            $rentalCarModel = new RentalCarModel();
            $rentalCarModel->adminAddCar($data);
            $this->success('添加成功!', url('AdminCar/index'));
        }

    }

    /**
     * 编辑汽车
     * @adminMenu(
     *     'name'   => '编辑汽车',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '编辑汽车',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $rentalCarModel = new RentalCarModel();
        $car = $rentalCarModel->where('id', $id)->find();
        $this->assign('car', $car);
        $this->getSelectAttr();
        return $this->fetch();
    }

    /**
     * 编辑汽车提交
     * @adminMenu(
     *     'name'   => '编辑汽车提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '编辑汽车提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $result = $this->validate($data, 'RentalCar');
            if ($result !== true) {
                $this->error($result);
            }
            $rentalCarModel = new RentalCarModel();
            $rentalCarModel->adminEditCar($data);
            $this->success('保存成功!', url('AdminCar/index'));

        }
    }

    /**
     * 汽车删除
     * @adminMenu(
     *     'name'   => '汽车删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '汽车删除',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $param = $this->request->param();
        $rentalCarModel = new RentalCarModel();

        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            $result = $rentalCarModel->where(['id' => $id])->find();
            $resultPortal = $rentalCarModel
                ->where(['id' => $id])
                ->update(['delete_time' => time()]);
            if ($resultPortal) {
                $this->success("删除成功！", '');
            }
            $this->error("删除失败！", '');
        }

        if (isset($param['ids'])) {
            $ids = $this->request->param('ids/a');
            $recycle = $rentalCarModel->where(['id' => ['in', $ids]])->select();
            $result = $rentalCarModel->where(['id' => ['in', $ids]])->update(['delete_time' => time()]);
            if ($result) {
                $this->success("删除成功！", '');
            }
            $this->error("删除失败！", '');
        }
    }

    /**
     * 汽车发布
     * @adminMenu(
     *     'name'   => '汽车发布',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '汽车发布',
     *     'param'  => ''
     * )
     */
    public function publish()
    {
        $param = $this->request->param();
        $rentalCarModel = new RentalCarModel();

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $rentalCarModel->where(['id' => ['in', $ids]])->update(['post_status' => 1]);

            $this->success("发布成功！", '');
        }

        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $rentalCarModel->where(['id' => ['in', $ids]])->update(['post_status' => 0]);

            $this->success("取消发布成功！", '');
        }

    }

    /**
     * 汽车推荐
     * @adminMenu(
     *     'name'   => '汽车推荐',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '汽车推荐',
     *     'param'  => ''
     * )
     */
    public function recommend()
    {
        $param = $this->request->param();
        $rentalCarModel = new RentalCarModel();

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');

            $rentalCarModel->where(['id' => ['in', $ids]])->update(['recommended' => 1]);

            $this->success("推荐成功！", '');

        }
        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');

            $rentalCarModel->where(['id' => ['in', $ids]])->update(['recommended' => 0]);

            $this->success("取消推荐成功！", '');

        }
    }

    /**
     * 汽车排序
     * @adminMenu(
     *     'name'   => '汽车排序',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '汽车排序',
     *     'param'  => ''
     * )
     */
    public function listOrder()
    {
        parent::listOrders(Db::name('portal_category_post'));
        $this->success("排序更新成功！", '');
    }
}
